<?php
/*********************************************************************************
 * SugarCRM Community Edition is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 ********************************************************************************/

require_once('include/MVC/View/views/view.list.php');

class CustomViewList extends ViewList{

    function CustomViewList(){
        parent::ViewList();
    }
    function prepareSearchForm() {
		global $app_list_strings, $beanList;

		if (isset($beanList['SecurityGroups'])) {
				$securitygroup_list = $this->get_securitygroup_list();
				asort($securitygroup_list);
				$app_list_strings['security_group_list'] = $securitygroup_list; 
		} else {
			$app_list_strings['security_group_list'] = array();
		}
		
		$type_list = $app_list_strings['authorpreferences_type_list'];
		asort($type_list);
		$app_list_strings['authorpreferences_type_list']=$type_list;
		
   		 $this->searchForm = null;

        //search
        $view = 'basic_search';
        if(!empty($_REQUEST['search_form_view']) && $_REQUEST['search_form_view'] == 'advanced_search')
            $view = $_REQUEST['search_form_view'];
        $this->headers = true;
        if(!empty($_REQUEST['search_form_only']) && $_REQUEST['search_form_only'])
            $this->headers = false;
        elseif(!isset($_REQUEST['search_form']) || $_REQUEST['search_form'] != 'false') {
            if(isset($_REQUEST['searchFormTab']) && $_REQUEST['searchFormTab'] == 'advanced_search') {
                $view = 'advanced_search';
            }else {
                $view = 'basic_search';
            }
        }

        $this->use_old_search = true;
        if ((file_exists('modules/' . $this->module . '/SearchForm.html')
                && !file_exists('modules/' . $this->module . '/metadata/searchdefs.php'))
            || (file_exists('custom/modules/' . $this->module . '/SearchForm.html')
                && !file_exists('custom/modules/' . $this->module . '/metadata/searchdefs.php'))){
            require_once('include/SearchForm/SearchForm.php');
            $this->searchForm = new SearchForm($this->module, $this->seed);
        }else{
            $this->use_old_search = false;
            require_once('custom/include/SearchForm/CustomSearchForm.php');

            if(file_exists('custom/modules/'.$this->module.'/metadata/metafiles.php')){
                require('custom/modules/'.$this->module.'/metadata/metafiles.php');
            }elseif(file_exists('modules/'.$this->module.'/metadata/metafiles.php')){
                require('modules/'.$this->module.'/metadata/metafiles.php');
            }

/*          if(!empty($metafiles[$this->module]['searchdefs']))
                require_once($metafiles[$this->module]['searchdefs']);
            elseif(file_exists('modules/'.$this->module.'/metadata/searchdefs.php'))
                require_once('modules/'.$this->module.'/metadata/searchdefs.php');
*/

			/* BEGIN - SECURITY GROUPS */ 
			//get group ids of current user and check to see if a layout exists for that group
			global $current_user;
			if (isset($beanList['SecurityGroups'])) {
				require_once('modules/SecurityGroups/SecurityGroup.php');
				$groupFocus = new SecurityGroup();
				$groupList = $groupFocus->getUserSecurityGroups($current_user->id);
				//reorder by precedence....
	
				foreach($groupList as $groupItem) {
					$GLOBALS['log']->debug("Looking for: ".'custom/modules/' . $this->module . '/metadata/'.$groupItem['id'].'/searchdefs.php');
					if(file_exists('custom/modules/' . $this->module . '/metadata/'.$groupItem['id'].'/searchdefs.php')){
						$_SESSION['groupLayout'] = $groupItem['id'];
						$metadataFile = 'custom/modules/' . $this->module . '/metadata/'.$groupItem['id'].'/searchdefs.php';
					}			
				}
			}
			if(isset($metadataFile)){
				$foundViewDefs = true;
                require_once($metadataFile);
			}
			else
			/* END - SECURITY GROUPS */     
            if (file_exists('custom/modules/'.$this->module.'/metadata/searchdefs.php'))
            {
                require_once('custom/modules/'.$this->module.'/metadata/searchdefs.php');
            }
            elseif (!empty($metafiles[$this->module]['searchdefs']))
            {
                require_once($metafiles[$this->module]['searchdefs']);
            }
            elseif (file_exists('modules/'.$this->module.'/metadata/searchdefs.php'))
            {
                require_once('modules/'.$this->module.'/metadata/searchdefs.php');
            }


            if(!empty($metafiles[$this->module]['searchfields']))
                require_once($metafiles[$this->module]['searchfields']);
            elseif(file_exists('modules/'.$this->module.'/metadata/SearchFields.php'))
                require_once('modules/'.$this->module.'/metadata/SearchFields.php');

            if(file_exists('custom/modules/'.$this->module.'/metadata/SearchFields.php')){
                require_once('custom/modules/'.$this->module.'/metadata/SearchFields.php');
            }


            $this->searchForm = new CustomSearchForm($this->seed, $this->module, $this->action);
            $this->searchForm->setup($searchdefs, $searchFields, 'include/SearchForm/tpls/SearchFormGeneric.tpl', $view, $this->listViewDefs);
            $this->searchForm->lv = $this->lv;
        }
    
	}
	
	 function listViewProcess(){
        $this->processSearchForm();
        $this->lv->searchColumns = $this->searchForm->searchColumns;

        if(!$this->headers)
            return;
        if(empty($_REQUEST['search_form_only']) || $_REQUEST['search_form_only'] == false){
            $this->lv->ss->assign("SEARCH",true);
            $this->custom_query();
            $this->lv->lvd->count_query = "SELECT COUNT(*) c from edanz_authorpreferences where edanz_authorpreferences.deleted = 0";
            $this->lv->setup($this->seed, 'include/ListView/ListViewGeneric.tpl', $this->where, $this->params);
            $savedSearchName = empty($_REQUEST['saved_search_select_name']) ? '' : (' - ' . $_REQUEST['saved_search_select_name']);
            echo $this->lv->display();
        }
    }
     private function get_securitygroup_list() {
		global  $app_list_strings;
		
		$security_group_list = array();
		
		$securitygroup = new SecurityGroup();
		$list = $securitygroup->get_full_list();
		foreach ($list as $grouplist) {
				if (!empty($grouplist)) {
					$new_grouplist = $grouplist->name;
					$security_group_list[$new_grouplist] = $new_grouplist;
				}
		}

	    return $security_group_list;
	}
      function custom_query() {
	            $this->params['custom_select'] = ", edanz_jobs.id as relate_job_id,edanz_jobs.name as relate_job";
	            $this->params['custom_from'] = " LEFT JOIN (select edanz_deliverables_authorpreferences.deliverables_id,edanz_deliverables_authorpreferences.authorpreferences_id,edanz_deliverables.date_modified,edanz_deliverables.job_id from edanz_deliverables_authorpreferences left join edanz_deliverables on edanz_deliverables.id = edanz_deliverables_authorpreferences.deliverables_id and edanz_deliverables_authorpreferences.deleted=0 order by edanz_deliverables_authorpreferences.authorpreferences_id,edanz_deliverables.date_modified desc)b ON edanz_authorpreferences.id = b.authorpreferences_id" .
	            							   " LEFT JOIN edanz_jobs ON edanz_jobs.id = b.job_id AND edanz_jobs.deleted = 0" ;
	            $this->params['custom_where'] = " GROUP BY edanz_authorpreferences.id ";
	            $this->params['custom_order_by'] = " ";
		}
    
}
?>
